﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base.Currency;
    using UFIDA.U9.Base.Organization;
    using UFIDA.U9.Base.UOM;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.CBO.SCM.Item;
    using UFIDA.U9.CBO.SCM.Supplier;
    using UFIDA.U9.PPR.PurPriceList;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// PurPriceListSaveSV partial 
	/// </summary>	
	public partial class PurPriceListSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new PurPriceListSaveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class PurPriceListSaveSVImpementStrategy : BaseStrategy
	{
		public PurPriceListSaveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			PurPriceListSaveSV bpObj = (PurPriceListSaveSV)obj;

			try
			{
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");

				List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

				foreach (var dto in bpObj.Data)
				{
					PurPriceList item = PurPriceList.Finder.Find($"Code='{dto.Code}'");
					if (item == null)
					{
						//新增逻辑
						using (ISession se = Session.Open())
						{
							item = PurPriceList.Create();
							this.SetValues(item, dto);
							se.Commit();
						}
					}
					else
					{
						//修改逻辑
						throw new AITException(599,$"单号:[{dto.Code}]已经存在,不能生成!");
					}

					retlist.Add(new CommonArchiveDataDTOData
					{
						ID = item.ID,
						Code = item.Code
					}) ;
				}
				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch (Exception ex)
			{
				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}

        private void SetValues(PurPriceList item, PurPriceListAITDTOData dto)
        {
			if (dto.PurPriceLines == null || dto.PurPriceLines.Count <= 0)
				throw new Exception("传入参数行为空");

            item.Code = dto.Code;
			item.Supplier = EntityUtils.GetEntityByCode<Supplier>(dto.Supplier.Code);
			item.Org = EntityUtils.GetEntityByCode<Organization>(dto.Org.Code);
			item.IsIncludeTax = dto.IsIncludeTax;
			item.Currency = EntityUtils.GetEntityByCode<Currency>(dto.Currency.Code);
			foreach(var linedto in dto.PurPriceLines)
            {
				PurPriceLine line= PurPriceLine.Create(item);
				line.ToDate=DateTime.Parse(linedto.ToDate);
				line.FromDate=DateTime.Parse(linedto.FromDate);
				line.Price = linedto.Price;
                ItemMaster itemMaster = EntityUtils.GetEntityByCode<ItemMaster>(linedto.ItemInfo.ItemCode);
                if (itemMaster == null)
                {
					throw new Exception($"料号查找失败,料号:[{line.ItemInfo.ItemCode}]");
                }
                line.ItemInfo = new UFIDA.U9.CBO.SCM.Item.ItemInfo
				{
					ItemID = itemMaster,
					ItemCode = itemMaster.Code,
					ItemName = itemMaster.Name,
				};
				line.Uom = itemMaster.PriceUOM;
				line.DocLineNo = linedto.DocLineNo;
				line.Active = linedto.Active;
            }
		}
    }

	#endregion
	
	
}